package com.zhiyou.servlet.repair;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou.model.Repair;
import com.zhiyou.service.RepairService;
import com.zhiyou.serviceImpl.RepairServiceImpl;

/**
 * Servlet implementation class RepairUpdateServlet
 */
@WebServlet("/RepairUpdateServlet")
public class RepairUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RepairUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		RepairService rs = new RepairServiceImpl();
		int fr_id = Integer.parseInt(request.getParameter("fr_id"));
		int flat_id = Integer.parseInt(request.getParameter("flat_id"));
		int lo_id = Integer.parseInt(request.getParameter("lo_id"));
		String fr_explain = request.getParameter("fr_explain");
		SimpleDateFormat spl = new SimpleDateFormat("yyyy-MM-dd");
		String time = request.getParameter("fr_time");
		Date fr_time = null ;
		try {
			fr_time = spl.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fr_result = request.getParameter("fr_result");
		String fr_remark = request.getParameter("fr_remark");
		Repair repair = new Repair(fr_id, flat_id, lo_id, fr_explain, fr_time, fr_result, fr_remark);
		boolean flag = rs.update(repair);
		if(flag){
			request.setAttribute("msg", "创建成功");
			request.getRequestDispatcher("/RepairShowServlet?1=1").forward(request, response);
		}else{
			request.setAttribute("msg", "创建失败");
			request.getRequestDispatcher("/view/repair/update.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
